package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.activity.huashigang.HuaShiGangFloorConfig;
import com.yanqu.road.entity.activity.stronghold.config.StrongholdDonateConfig;
import com.yanqu.road.entity.activity.stronghold.config.StrongholdTimeConfig;
import com.yanqu.road.entity.activity.stronghold.config.StrongholdTradeConfig;
import com.yanqu.road.entity.activity.stronghold.config.StrongholdWildConfig;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.pb.activity.StrongholdActivityProto;
import com.yanqu.road.pb.commonactivity.CommonActivityProto;
import com.yanqu.road.server.logic.activity.stronghold.StrongholdActivityData;
import com.yanqu.road.server.manger.ServerLanguageMgr;

import java.util.List;
import java.util.Map;

public class StrongholdActivityPb {

    public static StrongholdActivityProto.SyncStrongholaActivityMsg.Builder getStrongholdActivityMsg(ActivityInfo activityInfo, Map<Integer, HuaShiGangFloorConfig> desertConfigMap, Map<Integer, StrongholdDonateConfig> donateConfigMap, Map<Integer, List<ActivityConditionInfo>> conditionInfoMap, Map<String, String> paramConfigMap, Map<Integer, StrongholdTimeConfig> timeConfigMap, Map<Integer, StrongholdTradeConfig> tradeConfigMap, Map<Integer, StrongholdWildConfig> wildConfigMap, Map<Integer, MallInfo> mallInfoMap, Map<Integer, StrongholdActivityData> tempMap, String language) {
        StrongholdActivityProto.SyncStrongholaActivityMsg.Builder syncMsg = StrongholdActivityProto.SyncStrongholaActivityMsg.newBuilder();
        StrongholdActivityProto.StrongholdActivityMsg.Builder msg = StrongholdActivityProto.StrongholdActivityMsg.newBuilder();
        msg.setActivityInfo(getStrongholdActivityTempMsg(activityInfo, language));
        for (List<ActivityConditionInfo> conditionInfoList : conditionInfoMap.values()) {
            for (ActivityConditionInfo conditionInfo : conditionInfoList) {
                msg.addActivityConditionInfoList(CommonActivityPb.parseCommonActivityConditionTempMsg(conditionInfo, language));
            }
        }
        if (mallInfoMap != null) {
            for (MallInfo mallInfo : mallInfoMap.values()) {
                msg.addActivityMallInfoList(CommonPb.parseMallTempMsg(mallInfo, language));
            }
        }
        for (HuaShiGangFloorConfig desertConfig : desertConfigMap.values()) {
            msg.addDesertFloor(parseDesertFloorTempMsg(desertConfig));
        }
        for (StrongholdTimeConfig timeConfig : timeConfigMap.values()) {
            msg.addTimeInfo(parseTimeConfigTempMsg(timeConfig));
        }
        for (StrongholdWildConfig wildConfig : wildConfigMap.values()) {
            msg.addHouseInfo(parseHouseInfoTempMsg(wildConfig, language));
        }
        for (StrongholdDonateConfig donateConfig : donateConfigMap.values()) {
            msg.addDonateInfo(parseDonageTempMsg(donateConfig));
        }
        for (StrongholdTradeConfig tradeConfig : tradeConfigMap.values()) {
            msg.addTradeInfo(parseTradeTempMsg(tradeConfig, language));
        }
        for (Map.Entry<String, String> entry : paramConfigMap.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            StrongholdActivityProto.StrongholdConfigTempMsg.Builder param = StrongholdActivityProto.StrongholdConfigTempMsg.newBuilder();
            param.setParam(key);
            param.setValue(value);
            msg.addConfigInfo(param);
        }

        if (tempMap != null) {
            StrongholdActivityProto.StrongholdActivityDataMsg.Builder detailMsg = parseActivityDetailMsg(tempMap);
            msg.setActivityData(detailMsg);
        }
        syncMsg.setMsg(msg);
        return syncMsg;
    }

    private static StrongholdActivityProto.StrongholdTradeTempMsg.Builder parseTradeTempMsg(StrongholdTradeConfig tradeConfig, String language) {
        StrongholdActivityProto.StrongholdTradeTempMsg.Builder msg = StrongholdActivityProto.StrongholdTradeTempMsg.newBuilder();
        msg.setId(tradeConfig.getId());
        msg.setName(ServerLanguageMgr.getContent(tradeConfig.getName(), language));
        msg.setDesc(ServerLanguageMgr.getContent(tradeConfig.getDesc(), language));
        msg.setConsume(tradeConfig.getConsume());
        msg.setRewards(tradeConfig.getRewards());
        msg.setRankRange(tradeConfig.getRankRange());
        return msg;
    }

    private static StrongholdActivityProto.StrongholdDonateTempMsg.Builder parseDonageTempMsg(StrongholdDonateConfig donateConfig) {
        StrongholdActivityProto.StrongholdDonateTempMsg.Builder msg = StrongholdActivityProto.StrongholdDonateTempMsg.newBuilder();
        msg.setLevel(donateConfig.getLevel());
        msg.setIcon(donateConfig.getIcon());
        msg.setHpAdd(donateConfig.getHpAdd());
        msg.setNeedProcess(donateConfig.getNeedProcess());
        return msg;
    }

    private static StrongholdActivityProto.StrongholdHouseTempMsg.Builder parseHouseInfoTempMsg(StrongholdWildConfig wildConfig, String language) {
        StrongholdActivityProto.StrongholdHouseTempMsg.Builder msg = StrongholdActivityProto.StrongholdHouseTempMsg.newBuilder();
        msg.setId(wildConfig.getId());
        msg.setName(ServerLanguageMgr.getContent(wildConfig.getName(), language));
        msg.setType(wildConfig.getType());
        msg.setIcon(wildConfig.getIcon());
        msg.setSort(wildConfig.getSort());
        return msg;
    }

    private static StrongholdActivityProto.StrongholdTimeTempMsg.Builder parseTimeConfigTempMsg(StrongholdTimeConfig timeConfig) {
        StrongholdActivityProto.StrongholdTimeTempMsg.Builder msg = StrongholdActivityProto.StrongholdTimeTempMsg.newBuilder();
        msg.setTimeId(timeConfig.getTimeId());
        msg.setType(timeConfig.getType());
        msg.setBeginTime(timeConfig.getBeginTime());
        msg.setDurationTime(timeConfig.getDurationTime());
        msg.setParam(timeConfig.getParam());
        return msg;
    }

    private static StrongholdActivityProto.StrongholdDesertFloorTempMsg.Builder parseDesertFloorTempMsg(HuaShiGangFloorConfig config) {
        StrongholdActivityProto.StrongholdDesertFloorTempMsg.Builder msg = StrongholdActivityProto.StrongholdDesertFloorTempMsg.newBuilder();
        msg.setFloor(config.getFloor());
        msg.setSpecialReward(config.getSpecialReward());
        msg.setRewards(config.getRewards());
        msg.setConsume(config.getConsume());
        return msg;
    }


    public static StrongholdActivityProto.StrongholdActivityTempMsg.Builder getStrongholdActivityTempMsg(ActivityInfo activityInfo, String language) {
        StrongholdActivityProto.StrongholdActivityTempMsg.Builder msg = StrongholdActivityProto.StrongholdActivityTempMsg.newBuilder();
        msg.setActivityId(activityInfo.getActivityId());
        msg.setActivityName(ServerLanguageMgr.getContent(activityInfo.getActivityName(), language));
        msg.setDescription(ServerLanguageMgr.getContent(activityInfo.getActivityDesc(), language));
        msg.setBeginShowTime(activityInfo.getBeginShowTime());
        msg.setEndShowTime(activityInfo.getEndShowTime());
        msg.setBeginTime(activityInfo.getBeginTime());
        msg.setEndTime(activityInfo.getEndTime());
        msg.setIcon(activityInfo.getIcon());
        msg.setPic(activityInfo.getParam1());
        msg.addAllServerList(activityInfo.getServerIdList());
        msg.setChildType(activityInfo.getChildType());
        msg.setActivityType(activityInfo.getType());
        msg.setUnionGroup(activityInfo.getUnionGroup());
        return msg;
    }

    public static CommonActivityProto.CommonActivityDataMsg.Builder parseCommonActivityDataMsg(StrongholdActivityData activityData) {
        if (null == activityData) {
            return null;
        }
        CommonActivityProto.CommonActivityDataMsg.Builder msg = CommonActivityProto.CommonActivityDataMsg.newBuilder();
        for (UserActivityConditionData conditionData : activityData.getUserActivityConditionDataList()) {
            CommonActivityProto.CommonActivityConditionDataMsg.Builder conditionDataMsg = CommonActivityPb.parseCommonActivityConditionDataMsg(conditionData);
            if (null != conditionDataMsg) {
                msg.addConditionDataList(conditionDataMsg);
            }
        }
        return msg;
    }

    public static StrongholdActivityProto.StrongholdActivityDataMsg.Builder parseActivityDetailMsg(Map<Integer, StrongholdActivityData> dataMap) {
        //活动数据
        StrongholdActivityProto.StrongholdActivityDataMsg.Builder msg = StrongholdActivityProto.StrongholdActivityDataMsg.newBuilder();
        CommonActivityProto.CommonActivityDataMsg.Builder activityDataMsg = CommonActivityProto.CommonActivityDataMsg.newBuilder();
        for (StrongholdActivityData data : dataMap.values()) {
            for (UserActivityConditionData conditionData : data.getUserActivityConditionDataList()) {
                CommonActivityProto.CommonActivityConditionDataMsg.Builder conditionDataMsg = CommonActivityPb.parseCommonActivityConditionDataMsg(conditionData);
                if (null != conditionDataMsg) {
                    activityDataMsg.addConditionDataList(conditionDataMsg);
                }
            }
        }
        msg.setActivityData(activityDataMsg);
        return msg;
    }
}
